import logging

from base_test import TestBase

from django.core.urlresolvers import reverse
from friday.apps.groups.models import Group, Member


class BaseGroupTest(TestBase):
    
    PRE_GROUP_UID    = "vivelevendredi"

    PRE_GROUP_SLOGAN = "Pre Group slogan"
    PRE_GROUP_NAME   = "Pre Group name"
    PRE_GROUP_DESC   = "Pre Group desc"
    PRE_GROUP_GOOGLE = "Pre Group goolge"
    PRE_GROUP_OWNER  = TestBase.webmaster_email()

    PRE_MEMBER_EMAIL = "pre.member@gmail.com"
    
    def setUp(self):
        super(BaseGroupTest, self).setUp()
        # Prepare the pre group
        self.reset_user_to_webmaster()
        self.__class__.create_pre_group()
        self.pre_member = self.__class__.create_pre_member()

    def tearDown(self):
        user = super(BaseGroupTest, self).get_user(self.PRE_MEMBER_EMAIL)
        member = Member.get_unique(group=self.pre_group(), user=user)
        if member is not None:
            member.delete()
        group = Group.get_unique(uid=self.pre_group().uid)
        if group is not None:
            group.delete()
        super(BaseGroupTest, self).tearDown()

    @property
    def create_url(self):
        return reverse("friday.create_group")

    def view_url(self, uid):
        return reverse("friday.view_group", kwargs={"group_uid" : uid})

    @classmethod
    def pre_group(cls):
        return Group.get_unique(cls.PRE_GROUP_UID)

    @classmethod
    def create_pre_group(cls):
        pre_data = cls.get_pre_group_data()
        pre_data["uid"] = cls.PRE_GROUP_UID
        pre_data["creator"] = cls.get_user(pre_data["owner"])
        pre_data["owner"] = pre_data["creator"]
        group = Group.create(**pre_data)
        group.save()
        return group
        
    @classmethod
    def create_pre_member(cls):
        # member
        pre_data = {}
        pre_data["group"] = cls.pre_group()
        pre_data["user"] = cls.get_user(cls.PRE_MEMBER_EMAIL)
        pre_data["role"] = Member.MEMBER
        pre_data["is_approved"] = True
        member = Member.create(**pre_data)
        member.save()
        return member    

    @classmethod
    def get_pre_group_data(cls):
        pre_data = {  "name"         : cls.PRE_GROUP_NAME,
                      "slogan"       : cls.PRE_GROUP_SLOGAN,
                      "description"  : cls.PRE_GROUP_DESC,
                      "google_group" : cls.PRE_GROUP_GOOGLE,
                      "owner"        : cls.PRE_GROUP_OWNER, }
        return pre_data

    def get_update_group_data(self):
        update_data = { "name"         : "Group name for update",
                        "slogan"       : "Group slogan for update",
                        "description"  : "Group desc for update",
                        "google_group" : "Group goolge for update",
                        "owner"        : TestBase.webmaster_email(), }
        return update_data

 #EOF